package org.harsh.transmission.service.impl;

import cn.hutool.core.lang.Pair;
import com.ruoyi.common.core.text.Convert;
import org.apache.ibatis.annotations.Param;
import org.harsh.dispatch.CommVars;
import org.harsh.transmission.domain.TransTask;
import org.harsh.transmission.mapper.TransTaskMapper;
import org.harsh.transmission.service.ITransTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 传输任务Service业务层处理
 * 
 * @author liuhaibo
 * @date 2024-08-25
 */
@Service
public class TransTaskServiceImpl implements ITransTaskService 
{
    @Autowired
    private TransTaskMapper transTaskMapper;

    /**
     * 查询传输任务
     * 
     * @param taskId 传输任务主键
     * @return 传输任务
     */
    @Override
    public TransTask selectTransTaskByTaskId(Long taskId)
    {
        return transTaskMapper.selectTransTaskByTaskId(taskId);
    }

    @Override
    public TransTask selectTransTaskByMsgId(Long msgId){
        return transTaskMapper.selectTransTaskByMsgId(msgId);
    }

    @Override
    public TransTask selectTransTaskByMsgNodeId(Long msgId, long nodeId) {
        return transTaskMapper.selectTransTaskByMsgNodeId(msgId, nodeId);
    }

    /**
     * 查询传输任务状态列表
     * @param msgIdSet 消息编号集合
     * @return 传输任务状态集合
     */
    @Override
    public List<Pair<Integer,Integer>> selectTaskStatusListByMsgIdSet(List<Integer> msgIdSet){
        return transTaskMapper.selectTaskStatusListByMsgIdSet(msgIdSet);
    }

    /**
     * 查询未传输任务
     * @param size
     * @return
     */
    @Override
    public List<TransTask> selectUnTransTask(int size) {
        return transTaskMapper.selectUnTransTask(size);
    }


    public List<TransTask> selectUnuniTransTask(int size) {
        return transTaskMapper.selectUnuniTransTask(size);
    }

    /**
     * 查询节点未传输任务
     * @param size
     * @return
     */
    @Override
    public List<TransTask> selectUnTransTask(long recvNode, int size) {
        return transTaskMapper.selectNodeUnTransTask(recvNode, size);
    }

    /**
     * 查询未传输组播、广播
     * @param size
     * @return
     */
    @Override
    public List<TransTask> selectUnTransOtherTypeTask(int size) {
        return transTaskMapper.selectUnTransOtherTypeTask(size);
    }

    /**
     * 查询传输任务列表
     * 
     * @param transTask 传输任务
     * @return 传输任务
     */
    @Override
    public List<TransTask> selectTransTaskList(TransTask transTask)
    {
        return transTaskMapper.selectTransTaskList(transTask);
    }

    /**
     * 新增传输任务
     * 
     * @param transTask 传输任务
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTransTask(TransTask transTask)
    {
        return transTaskMapper.insertTransTask(transTask);
    }

    /**
     * 修改传输任务
     * 
     * @param transTask 传输任务
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTransTask(TransTask transTask)
    {
        return transTaskMapper.updateTransTask(transTask);
    }

    /**
     * 根据消息ID设置传输成功
     * @param msgId
     * @return
     */
    @Override
    @Transactional
    public int setTransSuccess(@Param("msgId") long msgId) {
        // 清除任务中缓存信息
        TransTask transTask = transTaskMapper.selectTransTaskByMsgId(msgId);
        CommVars.loadedTask.remove(transTask.getTaskId());
        return transTaskMapper.setTransSuccess(msgId);
    }

    /**
     * 根据消息ID 和节点ID 设置传输成功
     * @param msgId
     * @param nodeId
     * @return
     */
    @Override
    @Transactional
    public int setTransSuccess(long msgId, long nodeId) {
        TransTask transTask = transTaskMapper.selectTransTaskByMsgNodeId(msgId, nodeId);
        CommVars.loadedTask.remove(transTask.getTaskId());
        return transTaskMapper.setNodeTransSuccess(transTask.getTaskId());
    }

    /**
     * 批量删除传输任务
     * 
     * @param taskIds 需要删除的传输任务主键
     * @return 结果
     */
    @Override
    public int deleteTransTaskByTaskIds(String taskIds)
    {
        return transTaskMapper.deleteTransTaskByTaskIds(Convert.toStrArray(taskIds));
    }

    /**
     * 删除传输任务信息
     * 
     * @param taskId 传输任务主键
     * @return 结果
     */
    @Override
    public int deleteTransTaskByTaskId(Long taskId)
    {
        return transTaskMapper.deleteTransTaskByTaskId(taskId);
    }
}
